<template>
  <div class="com-container">
    <div class="com-chart" ref="seller"></div>
  </div>
</template>

<script>
import "../assets/js/essos.js";
export default {
  data() {
    return {
      allData: [],
      myCharts: null,
      pageNumber: 1,
      pageSize: 5,
      totalPages: 0,
      timerID: null,
    };
  },
  methods: {
    //初始化报表
    async initCharts() {
      this.myCharts = this.$echarts.init(this.$refs.seller, "essos");
      //初始化报表配置项，与数据无关
      const initOptions = {
        title: {
          text: "▎ 商家销量排行",
          left: 20,
          top: 20,
          textStyle: {
            color: "#893448",
            // fontSize: 43.2,
          },
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        series: [
          {
            type: "bar",
          },
        ],
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {color: "#faeff5" },
          },
        },
      };
      this.myCharts.setOption(initOptions);
      this.myCharts.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.myCharts.on("mouseout", () => {
        this.startTimer();
      });
    },
    //创建报表
    async createReports() {
      //获取数据
      const { data } = await this.$axios.get("/data");
      this.allData = data;
      //给获取到的数据按从小到大排序
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      //计算总页数
      this.totalPages =
        data.length % this.pageSize == 0
          ? parseInt(data.length / this.pageSize)
          : parseInt(data.length / this.pageSize) + 1;
      const start = (this.pageNumber - 1) * this.pageSize;
      const end = this.pageNumber * this.pageSize;
      const showData = this.allData.slice(start, end);
      var xData = showData.map((x) => {
        return x.name;
      });
      var yData = showData.map((y) => {
        return y.value;
      });
      //报表数据样式等设计
      var dataOptions = {
        yAxis: {
          data: xData,
        },
        series: [
          {
            data: yData,
            showBackground: true,
            backgroundStyle: {
              color: "rgba(180, 180, 180, 0.2)",
            },
            label: {
              show: true,
              position: "right",
              textStyle: { color: "#d95850" },
            },
            // barWidth: 43.2,
            itemStyle: {
              // barBorderRadius: [0, 21.6, 21.6, 0],
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                { offset: 0, color: "#d95850" },
                { offset: 1, color: "#eb8146" },
              ]),
            },
          },
        ],
      };
      this.myCharts.setOption(dataOptions);
    },
    //定时动画
    startTimer() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.pageNumber++;
        if (this.pageNumber > this.totalPages) {
          this.pageNumber = 1;
        }
        this.createReports();
      }, 3000);
    },
    // 当浏览器的大小发生变化的时候, 会调用的方法, 来完成屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.seller.offsetWidth / 100) * 3.6;     
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            },
          },
        ],
      };
      this.myCharts.setOption(adapterOption);
      this.myCharts.resize();
    },
  },
  mounted: function () {
    this.initCharts();
    this.createReports();
    this.startTimer();
     this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    clearInterval(this.timerId); // 在组件销毁的时候, 需要将监听器取消掉
    window.removeEventListener("resize", this.screenAdapter);
  },
};
</script>

<style>
.com-container{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.com-chart{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>